﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Repository.Azure.Table
{
    /// <summary>
    /// A repository that stores administrator account entities.
    /// </summary>
    public class AdministratorAccountRepository : TableRepositoryBase, IAdministratorAccountRepository
    {
        #region Fields

        private AdministratorAccountContext _context;

        #endregion

        #region Constructors

        public AdministratorAccountRepository(Dictionary<string, object> repositoryParameters)
            : base(repositoryParameters)
        {
            _context = new AdministratorAccountContext(StorageAccount.TableEndpoint.AbsoluteUri, StorageAccount.Credentials);
        }

        #endregion

        #region IAdministratorAccountRepository Members

        /// <summary>
        /// Inserts an administrator account entity into the repository.
        /// </summary>
        /// <param name="administratorAccount"></param>
        public void Insert(AdministratorAccount administratorAccount)
        {
            _context.Insert(CreateRowFromAdministratorAccount(administratorAccount));
        }

        /// <summary>
        /// Updtes an administrator account entity in the repository.
        /// </summary>
        /// <param name="administratorAccount"></param>
        public void Update(AdministratorAccount administratorAccount)
        {
            _context.Update(CreateRowFromAdministratorAccount(administratorAccount));
        }

        /// <summary>
        /// Deletes an administrator account entity from the repository.
        /// </summary>
        /// <param name="administratorAccount"></param>
        public void Delete(AdministratorAccount administratorAccount)
        {
            _context.Delete(CreateRowFromAdministratorAccount(administratorAccount));
        }

        /// <summary>
        /// Fetches all administrator account entities from the repository.
        /// </summary>
        /// <returns></returns>
        public List<AdministratorAccount> Fetch()
        {
            List<AdministratorAccountRow> rows = _context.GetRows();
            List<AdministratorAccount> administratorAccounts = null;

            if (rows != null)
            {
                administratorAccounts = new List<AdministratorAccount>();

                foreach (AdministratorAccountRow row in rows)
                    administratorAccounts.Add(CreateAdministratorAccountFromRow(row));
            }

            return administratorAccounts;
        }

        /// <summary>
        /// Fetches a single administrator account entity from the repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AdministratorAccount Fetch(Guid id)
        {
            AdministratorAccountRow row = _context.GetRow(id);

            if (row != null)
                return CreateAdministratorAccountFromRow(row);
            else
                return null;
        }

        /// <summary>
        /// Creates a row from the POCO object.
        /// </summary>
        /// <param name="administratorAccount"></param>
        /// <returns></returns>
        private AdministratorAccountRow CreateRowFromAdministratorAccount(AdministratorAccount administratorAccount)
        {
            return new AdministratorAccountRow()
            {
                ApplicationName = ApplicationName,
                EmailAddress = administratorAccount.EmailAddress,
                PartitionKey = ApplicationName,
                RowKey = administratorAccount.Id.ToString(),
                FirstName = administratorAccount.FirstName,
                LastName = administratorAccount.LastName,
                UserName = administratorAccount.UserName,
                Id = administratorAccount.Id,
            };
        }

        /// <summary>
        /// Creates a POCO object from the row.
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private AdministratorAccount CreateAdministratorAccountFromRow(AdministratorAccountRow row)
        {
            AdministratorAccount administratorAccount = new AdministratorAccount();

            administratorAccount.EmailAddress = row.EmailAddress;
            administratorAccount.FirstName = row.FirstName;
            administratorAccount.LastName = row.LastName;
            administratorAccount.UserName = row.UserName;
            administratorAccount.Id = row.Id;

            return administratorAccount;
        }

        #endregion
    }
}
